package deployments

/**
 * Author: 孙克旭_泺栋
 * Description: deployments路由规则
 * Date: 2025-09-27
 */

import (
	"kubernate-server/service"

	"github.com/gin-gonic/gin"
)

type K8SDeploymentsRouter struct {
}

func (*K8SDeploymentsRouter) InitK8SDeploymentsRouter(r *gin.Engine) {
	group := r.Group("/deploy")
	// 创建deploy
	group.POST("/", service.ApiGroupApp.CreateDeploy)
	// 删除指定的deploy
	group.DELETE("/", service.ApiGroupApp.DeleteDeploy)
	// 获取指定命名空间下deployments列表
	group.GET("/", service.ApiGroupApp.GetDeploy)
	// 查询全部deploy
	group.GET("/all", service.ApiGroupApp.GetAllDeploy)
	// 修改deploy
	group.PUT("/", service.ApiGroupApp.UpdateDeploy)
}

/**
测试案例：
1. 获取deploy列表：
	GET http://127.0.0.1:8082/deploy?namespace=kube-system
	结果：
{
    "code": 20000,
    "data": {
        "items": [
            {
                "name": "coredns",
                "namespace": "kube-system",
                "replicas": 2,
                "availableReplicas": 2,
                "readyReplicas": 2,
                "image": "registry.aliyuncs.com/google_containers/coredns:v1.8.6",
                "labels": {
                    "k8s-app": "kube-dns"
                },
                "creationTimestamp": "2025-09-19T06:42:39Z",
                "status": "Ready"
            }
        ],
        "total": 1
    },
    "message": "获取成功"
}

2. 创建deploy：
	POST {{baseURL}}/deploy/
	post体：json
	{
    "name": "test",
    "namespace": "default",
    "replicas": 3,
    "image": "registry.cn-hangzhou.aliyuncs.com/yinzhengjie-k8s/apps:v1",
    "containerName": "web",
    "ports": [
        {
            "name": "http",
            "containerPort": 80,
            "protocol": "TCP"
        }
    ],
    "env": [],
    "labels": {
        "app-type": "web-server",
        "version": "1.0"
    }
}
	结果：
{
    "code": 20000,
    "data": {
        "name": "test",
        "namespace": "default"
    },
    "message": "创建成功"
}

3. 修改deploy：根据name和名称空间查找deploy，只能修改副本数量和镜像
	PUT {{baseURL}}/deploy
	请求体：json
	{
    "name": "test",
    "namespace": "default",
    "replicas": 1,
    "image": "registry.cn-hangzhou.aliyuncs.com/yinzhengjie-k8s/apps:v1"
}
	结果：
	{
    "code": 20000,
    "data": {
        "name": "test",
        "namespace": "default"
    },
    "message": "更新成功"
}


4. 删除deploy：
	DELETE {{baseURL}}/deploy?name=test&namespace=default
	结果：
	{
    "code": 20000,
    "data": {
        "name": "test",
        "namespace": "default"
    },
    "message": "删除成功"
}
5. 获取全部的deploy
	GET {{baseURL}}/deploy/all
	结果：
{
    "code": 20000,
    "data": {
        "default": [
            {
                "name": "test",
                "namespace": "default",
                "replicas": 3,
                "availableReplicas": 3,
                "readyReplicas": 3,
                "images": [
                    "registry.cn-hangzhou.aliyuncs.com/yinzhengjie-k8s/apps:v3"
                ],
                "labels": {
                    "version": "1.0"
                },
                "creationTimestamp": "2025-10-01T13:34:20Z"
            },
            {
                "name": "test-1",
                "namespace": "default",
                "replicas": 2,
                "availableReplicas": 2,
                "readyReplicas": 2,
                "images": [
                    "registry.cn-hangzhou.aliyuncs.com/yinzhengjie-k8s/apps:v2"
                ],
                "labels": {
                    "version": "1.0"
                },
                "creationTimestamp": "2025-10-01T13:59:03Z"
            }
        ],
        "kube-public": [
            {
                "name": "xiuxian-oldboyedu-linux99",
                "namespace": "kube-public",
                "replicas": 3,
                "availableReplicas": 3,
                "readyReplicas": 3,
                "images": [
                    "registry.cn-hangzhou.aliyuncs.com/yinzhengjie-k8s/apps:v2"
                ],
                "labels": {
                    "app.kubernetes.io/instance": "xiuxian",
                    "app.kubernetes.io/managed-by": "Helm",
                    "app.kubernetes.io/name": "oldboyedu-linux99",
                    "app.kubernetes.io/version": "1.16.0",
                    "helm.sh/chart": "oldboyedu-linux99-0.1.0"
                },
                "creationTimestamp": "2025-10-01T11:18:43Z"
            }
        ],
        "kube-system": [
            {
                "name": "coredns",
                "namespace": "kube-system",
                "replicas": 2,
                "availableReplicas": 2,
                "readyReplicas": 2,
                "images": [
                    "registry.aliyuncs.com/google_containers/coredns:v1.8.6"
                ],
                "labels": {
                    "k8s-app": "kube-dns"
                },
                "creationTimestamp": "2025-09-19T06:42:39Z"
            },
            {
                "name": "csi-nfs-controller",
                "namespace": "kube-system",
                "replicas": 1,
                "availableReplicas": 1,
                "readyReplicas": 1,
                "images": [
                    "registry.k8s.io/sig-storage/csi-provisioner:v5.0.2",
                    "registry.k8s.io/sig-storage/csi-snapshotter:v8.0.1",
                    "registry.k8s.io/sig-storage/livenessprobe:v2.13.1",
                    "registry.k8s.io/sig-storage/nfsplugin:v4.9.0"
                ],
                "labels": null,
                "creationTimestamp": "2025-10-01T04:00:48Z"
            },
            {
                "name": "metrics-server",
                "namespace": "kube-system",
                "replicas": 2,
                "availableReplicas": 2,
                "readyReplicas": 2,
                "images": [
                    "registry.aliyuncs.com/google_containers/metrics-server:v0.7.2"
                ],
                "labels": {
                    "k8s-app": "metrics-server"
                },
                "creationTimestamp": "2025-09-29T02:43:20Z"
            }
        ],
        "kubernetes-dashboard": [
            {
                "name": "dashboard-metrics-scraper",
                "namespace": "kubernetes-dashboard",
                "replicas": 1,
                "availableReplicas": 1,
                "readyReplicas": 1,
                "images": [
                    "harbor250.oldboyedu.com/oldboyedu-add-ons/metrics-scraper:v1.0.7"
                ],
                "labels": {
                    "k8s-app": "dashboard-metrics-scraper"
                },
                "creationTimestamp": "2025-09-29T02:51:15Z"
            },
            {
                "name": "kubernetes-dashboard",
                "namespace": "kubernetes-dashboard",
                "replicas": 1,
                "availableReplicas": 1,
                "readyReplicas": 1,
                "images": [
                    "harbor250.oldboyedu.com/oldboyedu-add-ons/dashboard:v2.5.1"
                ],
                "labels": {
                    "k8s-app": "kubernetes-dashboard"
                },
                "creationTimestamp": "2025-09-29T02:51:15Z"
            }
        ],
        "metallb-system": [
            {
                "name": "controller",
                "namespace": "metallb-system",
                "replicas": 1,
                "availableReplicas": 1,
                "readyReplicas": 1,
                "images": [
                    "quay.io/metallb/controller:v0.15.2"
                ],
                "labels": {
                    "app": "metallb",
                    "component": "controller"
                },
                "creationTimestamp": "2025-09-24T07:34:55Z"
            }
        ]
    },
    "message": "获取成功"
}
*/
